package com.changgou.service.goods.service.impl;


import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.CategoryBrand;
import com.changgou.service.goods.dao.BrandMapper;
import com.changgou.service.goods.dao.CategoryBrandMapper;
import com.changgou.service.goods.service.BrandService;
import com.changgou.service.goods.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper,Brand> implements BrandService {

    @Autowired
    private BrandMapper brandMapper;


    @Resource
    private CategoryService categoryService;    // 用于查找分类的。


    @Resource
    private CategoryBrandMapper categoryBrandMapper;


    /**
     * 品牌列表查询
     * @return
     */
    @Override
    public List<Brand> findList() {

//        List<Brand> brandList = brandMapper.selectList();
        List<Brand> brandList = super.list();
        return brandList;
    }

    /**
     * 根据id查询品牌信息
     * @param id
     * @return
     */
    @Override
    public Brand findById(Integer id) {
//        Brand brand = brandMapper.selectByPrimaryKey(id);
        Brand brand = brandMapper.selectById(id);
        return brand;
    }

    /**
     * 品牌新增
     * @param brand
     */
    @Override
    @Transactional
    public void add(Brand brand) {
//        brandMapper.insertSelective(brand);
        brandMapper.insert(brand);
    }

    /**
     * 品牌修改
     * @param brand
     */
    @Override
    @Transactional
    public void update(Brand brand) {
//        brandMapper.updateByPrimaryKey(brand);
        brandMapper.updateById(brand);
    }

    /**
     * 根据id删除品牌信息
     * @param id
     */
    @Override
    @Transactional
    public void delById(Integer id) {
//        brandMapper.deleteByPrimaryKey(id);
        brandMapper.deleteById(id);
    }

    /**
     * 品牌列表条件查询
     * @param searchMap
     * @return
     */
    @Override
    public List<Brand> list(Map<String, Object> searchMap) {
//        Example example = new Example(Brand.class);
//        //封装查询条件
//        Example.Criteria criteria = example.createCriteria();
//        if (searchMap!=null){
//            //品牌名称(模糊) like  %
//            if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
//                criteria.andLike("name","%"+searchMap.get("name")+"%");
//            }
//            //按照品牌首字母进行查询(精确)
//            if (searchMap.get("letter")!=null && !"".equals(searchMap.get("letter"))){
//                criteria.andEqualTo("letter",searchMap.get("letter"));
//            }
//        }
//        List<Brand> brandList = brandMapper.selectByExample(example);
//        return brandList;
        return null;
    }

    @Override
    public Page<Brand> findPage(int page, int size) {
//        PageHelper.startPage(page,size);
//        Page<Brand> page1 = (Page<Brand>) brandMapper.selectAll();
        Page<Brand> brandPage = new Page<>(page, size);
        return this.page(brandPage);
    }

    @Override
    public Page<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
        //设置分页
//        PageHelper.startPage(page,size);

//        //设置查询条件
//        Example example = new Example(Brand.class);
//        Example.Criteria criteria = example.createCriteria();
//        if (searchMap!=null){
//            //设置品牌名称模糊查询
//            if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
//                criteria.andLike("name","%"+searchMap.get("name")+"%");
//            }
//            //设置品牌首字母的精确查询
//            if (searchMap.get("letter")!=null && !"".equals(searchMap.get("letter"))){
//                criteria.andEqualTo("letter",searchMap.get("letter"));
//            }
//        }
//        Page<Brand> pageInfo = (Page<Brand>) brandMapper.selectByExample(example);
//        return pageInfo;
        return null;
    }

    @Override
    public List<Brand> findListByCategoryName(String category) {
//        先根据 category名称查对应的id, 先注入category 的service。
//        LambdaQueryChainWrapper<Category> wrapper = this.categoryService.lambdaQuery().eq(Category::getName, category);

        QueryWrapper<Category> wrapper = new QueryWrapper<Category>().eq("name", category);

        Category category1 = this.categoryService.getOne(wrapper);
        if(ObjectUtil.isEmpty(category1)){
            System.out.println("没找到");
            return null;
        }

//        然后再去  中间表查  category_id 对应的brand_id , 注入中间表。

        QueryWrapper<CategoryBrand> wrapper1 =
                new QueryWrapper<CategoryBrand>()
                        .eq("category_id", category1.getId());

        List<CategoryBrand> categoryBrands = this.categoryBrandMapper.selectList(wrapper1);
        if(ObjectUtil.isEmpty(categoryBrands)){
            return  null;
        }

//        品牌列表集合。
        //        然后 根据brand_id  批量查询即可。 ，注入brand 的mapper。本身在 brand的service 直接用即可。
//        for (CategoryBrand categoryBrand : categoryBrands) {
//
//        }
//        把 categoryBrands 根据每个元素的属性brand_id 转换成 一个集合。
        List<Integer> brandIds = CollStreamUtil.toList(categoryBrands, CategoryBrand::getBrandId);

//        List<Brand> brandList = new ArrayList<>(categoryBrands.size());
//        然后根据 id 集合 批量查询集合
        List<Brand> brandList = this.brandMapper.selectBatchIds(brandIds);
        if(ObjectUtil.isEmpty(brandIds)){
            System.out.println("没找到品牌");
            return  null;
        }

        return brandList;
    }
}
